<?php

use Illuminate\Database\Seeder;
use App\Role;
use App\User;
use Illuminate\Support\Facades\DB;


class AdminSeeder extends Seeder{


    public function run(Request $request){

        /** @var User $user */
        $user = User::whereUsername('admin')->first();
        if(!$user){
            $user = User::firstOrCreate([
                'username' => 'admin',
                'password' => bcrypt('123456'),
                'user_key' => md5(rand(1000,9999) . time()),
                'role' => 1,
                'status'   => 1,
                'reg_ip'   => '127.0.0.1',
            ]);
        }

        /** @var Role $role */
        $role = Role::whereName('system_admin')->first();
        if(!$role){
            $role = Role::firstOrCreate([
                'name' => 'system_admin',
                'display_name' => '超级管理员',
            ]);
        }

        $roleUser = DB::table('role_user')->whereRoleId($role->id)->whereUserId($user->id)->first();
        if(!$roleUser){
            DB::table('role_user')->insert([
                'role_id' => $role->id,
                'user_id' => $user->id
            ]);
        }
    }

}
